package com.tn.utils;

import com.gitee.hifong45.Str;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName DateUtil
 * @Description TODO
 * @Author Yezhongzhu
 * @Date 2021/12/8 11:20
 * @Version v1.0
 **/
@Slf4j
public class DateUtil {
    public static final String FORMAT_YEAR_DAY_HOUR = "yyyy-MM-dd HH:mm:ss";

    public static final String FORMAT_YEAR_DAY_HOUR_F = "yyyy/MM/dd HH:mm:ss";

    public static final String FORMAT_YEAR_DAY_HOUR_N = "yyyyMMddHHmmss";

    public static SimpleDateFormat simpleDateFormat = null;

    public static Date stringToDate(String value){
        try {
            return DateUtils.parseDate(value,FORMAT_YEAR_DAY_HOUR);
        } catch (ParseException e) {
            log.error("stringToDate error:{}",e.getMessage(),e);
        }
        return null;
    }

    public static Date stringToDate(String value,String dateFormat){
        try {
            return DateUtils.parseDate(value,dateFormat);
        } catch (ParseException e) {
            log.error("stringToDate error:{}",e.getMessage(),e);
        }
        return null;
    }

    public static Long stringToDateLong(String value){
        try {
            return DateUtils.parseDate(value,FORMAT_YEAR_DAY_HOUR).getTime();
        } catch (ParseException e) {
            log.error("stringToDate error:{}",e.getMessage(),e);
        }
        return null;
    }

    public static Long stringToDateLong(String value,String dateFormat){
        try {
            return DateUtils.parseDate(value,dateFormat).getTime();
        } catch (ParseException e) {
            log.error("stringToDate error:{}",e.getMessage(),e);
        }
        return null;
    }

    public static String dateToString(Date date){
//        if(date == null) {
//            date = new Date(); // 当前
//        }
//        try {
//            simpleDateFormat = new SimpleDateFormat(FORMAT_YEAR_DAY_HOUR);
//            return simpleDateFormat.format(date);
//        } catch (Exception e) {
//            log.error("dateToString error:{}",e.getMessage(),e);
//        }
//        return null;
        return dateToString(date, FORMAT_YEAR_DAY_HOUR);
    }

    public static String dateToString(Date date,String format){
        if(date == null) {
            date = new Date(); // 当前
        }
        format = Str.orElse(format, FORMAT_YEAR_DAY_HOUR);
        try {
            simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.format(date);
        } catch (Exception e) {
            log.error("dateToString error:{}",e.getMessage(),e);
        }
        return null;
    }

    public static String getTodayStart(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        return dateToString(zero);
    }
}
